#
# Cookbook Name:: gitclientsetup
# Recipe:: default
#
# Copyright 2013, YOUR_COMPANY_NAME
#
# All rights reserved - Do Not Redistribute
#

cuser = node[:chef][:user]
cgroup = node[:chef][:group]
cuserhome = node[:chef][:userhome]
client_keyloc = node[:common][:client_keypair_loc]
ws_pubkey_loc = node[:common][:ws_pubkey_loc_at_cli]

group "#{cgroup}" do
  action :create
end

user "#{cuser}" do
  gid "#{cgroup}"
  action :create
end


bash "add #{cuser} to sudoers" do
  user "root"
  creates "/tmp/setup_sudoers.done"
  code <<-EOH
  	if [ -f "/etc/sudoers.tmp" ]; then
  	    exit 1
  	fi
  	cp /etc/sudoers /tmp/sudoers.new
  	echo '#{cuser}    ALL=(ALL)   NOPASSWD:    ALL' >> /tmp/sudoers.new
  	echo 'Defaults:#{cuser} !requiretty' >> /tmp/sudoers.new
  	echo 'Defaults:root !requiretty' >> /tmp/sudoers.new
  	visudo -c -f /tmp/sudoers.new
  	if [ "$?" -eq "0" ]; then
  	  cp /tmp/sudoers.new /etc/sudoers
      touch /tmp/setup_sudoers.done
  	else
  	  exit 1
  	fi
  	rm /tmp/sudoers.new
  EOH
end
    
# root's authorized_keys already has the ws-server's public key (we added the key while provisioning
# through softlayer APIs remember?). We now need to add that key to #{cuser}'s auth_keys file as well. 
# Also, we need to set the private key of the keypair, generated by the provisioningBuild script (at 
# /root/client) as default identity keys for #{cuser}
bash "setup password-less logon for #{cuser}" do
  user "root"
  creates "/tmp/pwlesslogon.done"
  code <<-EOH
    set -e
    
    mkdir -p #{cuserhome}/.ssh
    cp #{client_keyloc} #{cuserhome}/.ssh/id_rsa
    cp #{client_keyloc}.pub #{cuserhome}/.ssh/id_rsa.pub
    cat #{ws_pubkey_loc} >> #{cuserhome}/.ssh/authorized_keys
    chown -R #{cuser}:#{cgroup} #{cuserhome}/.ssh
    chmod -R 600 #{cuserhome}/.ssh
    chmod +x #{cuserhome}/.ssh
    touch /tmp/pwlesslogon.done
  EOH
end

template "#{cuserhome}/.ssh/known_hosts" do
  source "known_hosts.erb"
  mode 00600
  owner cuser
  group cgroup
  variables  :fileserverip => node[:common][:scpip], :fileservername => node[:common][:scphost],:fileserverid => node[:common][:scphost_key]
  action :create
end

bash "change_hosts_perms" do
  user "root"
  code <<-EOH
  chmod +r /etc/hosts
  EOH
end

startinstr "setup" do
  init true
end

cmdout = `grep "^\*.*soft.*nofiles.*16384" /etc/security/limits.conf`
if $? != 0
  bash "Update /etc/security/limits.conf for more number of open files" do
    user "root"
    code <<-EOH
    echo "*     soft    nofile    16384" >> /etc/security/limits.conf
    echo "*     hard    nofile    65535" >> /etc/security/limits.conf
    EOH
  end
end

cmdout = `grep "session required /lib/security/pam_limits.so" /etc/pam.d/login`
if $? != 0
  bash "Update /etc/pam.d/login for more number of  open files" do
    user "root"
    code <<-EOH
    echo "session required /lib/security/pam_limits.so" >> /etc/pam.d/login
    EOH
  end
end

epelurl = node[:rh54][:epelrepo]
if /6./ =~ node[:platform_version] 
  epelurl = node[:rh6][:epelrepo]
  bash "pickup_32bitpackages" do
    user "root"
    code <<-EOH
    echo "\nmultilib_policy=all\n" >> /etc/yum.conf
    EOH
  end
end

bash "install_epel_repo" do 
  user "root"
  code <<-EOH
  rpm -q epel-release-6-8.noarch
  if [ $? -ne 0 ]
  then
    if [ -f "/etc/yum.repos.d/Installation media.repo" ]
    then
      mv "/etc/yum.repos.d/Installation media.repo" "/etc/yum.repos.d/Installation media.repo.orig"
    fi
    rpm -Uvh #{epelurl}    
  fi 
  EOH
  
end

# libstdc++.so.5 *@#$#!!!#$ - 32-bit IHS @#$#$!!
%w(libstdc++ compat-db pam libstdc++.so.5 patch).each do |package|
  rpmout = `rpm -qi #{package}`
  if $? != 0
    package "#{package}" do
      action :install
    end
  end
end

# Allow loopback traffic - required for was.status.socket (WAS startup)
bash "allow_local_traffic" do
  user "root"
  code <<-EOH
    /sbin/iptables -I INPUT 1 -i lo -j ACCEPT
    /sbin/service iptables save 
  EOH
  not_if "/sbin/iptables -L -v | grep 'ACCEPT .* lo   '" 
end

bash "create #{node[:common][:inst_temp]}" do
  user "root"
  code <<-EOH
    mkdir -p #{node[:common][:inst_temp]} && chmod 777 #{node[:common][:inst_temp]}
  EOH
end

if node[:common][:useTempHddForInstallers]
  bash "initialize the extra tmp hdd - create partition, format and mount" do
    user "root"
    code <<-EOH
      set -e
      (echo o; echo n; echo p; echo 1; echo ; echo; echo w) | fdisk /dev/xvdc
      mkfs.ext4 /dev/xvdc1
      mount /dev/xvdc1 #{node[:common][:inst_temp]}
    EOH
  end
end

stopinstr "setup"
